<script src="js/chosen.jquery.js" type="text/javascript"></script>
        <script type="text/javascript" charset="utf-8">
			$(document).ready(function() {
				$("#tgl_perbaikan").datepicker({
					dateFormat  : "yy-mm-dd",        
					  changeMonth : true,
					  changeYear  : true					
				});
				
					});
</script>

<?php
if($_SESSION['sisa']!=1){
	echo "Akses tidak diizinkan";
	exit();
}

$act = filter($_GET['act']);
$field = filter($_GET['field']);
$q = filter($_GET['q']);

if(empty($act)){	
?>		
					<!-- box / title -->
					<div class="title">
						<h5>Data Perbaikan</h5>								
						<div class="search global-nav"></div>
					</div>
					<!-- end box / title -->
					<div class="table">										
						<table>
							<thead>
								<tr>
									<th width="20" class="selected"><input type="checkbox" class="checkall" /></th>
									<th width="24" class="action">No.</th>
									<th width="102" class="left">Nota Penjualan</th>
                                    <th width="102" class="left">Jenis Barang</th>
                                    <th width="102" class="left">Tanggal Penjualan</th>
                                    <th width="82" class="left">
Cup Ok Dari Rejeck</th>
                                    <th width="82" class="left">Cup Rusak Dari Rejeck</th>
                                    								
								</tr>
							</thead>
							<tbody>
							<?php			

							 
							$sqlKode1 = mysql_query("SELECT
tb_penjualan.nota,
tb_penjualan.tgl_jual,
tb_detail_penjualan.id_jns_brg,
tb_detail_penjualan.jumlah_cup_ok,
tb_detail_penjualan.jumlah_rusak_dus,
tb_detail_penjualan.satuan_rusak_dus,
tb_detail_penjualan.jumlah_rusak_cup,
tb_detail_penjualan.satuan_rusak_cup
FROM tb_penjualan,tb_detail_penjualan
WHERE tb_penjualan.id_penjualan = tb_detail_penjualan.id_penjualan");
							
							$no=1;
							while($dataKode = mysql_fetch_array($sqlKode1)){								

								$Penjualan=mysql_fetch_array(mysql_query("SELECT
tb_penjualan.nota,
tb_penjualan.tgl_jual,
tb_detail_penjualan.id_jns_brg,
tb_detail_penjualan.jumlah_cup_ok,
tb_detail_penjualan.jumlah_rusak_dus,
tb_detail_penjualan.satuan_rusak_dus,
tb_detail_penjualan.jumlah_rusak_cup,
tb_detail_penjualan.satuan_rusak_cup
FROM tb_penjualan,tb_detail_penjualan
WHERE tb_penjualan.id_penjualan = tb_detail_penjualan.id_penjualan"))
							?>
								<tr>
									<td class="selected"><input type="checkbox" /></td>
									<td class="center"><?php echo $no;?></td>
									<td  style="background-color:#FCF"><?php echo $Penjualan['nota'];?></td>
                                    <?php $nama=mysql_fetch_array(mysql_query("SELECT nama_brg FROM tb_jns_barang WHERE id_jns_brg='$Penjualan[id_jns_brg]'"))?>
                                    <td  style="background-color:#FCF"><?php echo $nama['nama_brg'];?></td>
                                    <td  style="background-color:#FCF"><?php echo $Penjualan['tgl_jual'];?></td>
                                    <td  style="background-color: #FCF"><?php echo $dataKode['jumlah_cup_ok'];?> Cup</td>
                                    <td  style="background-color: #FCF"><?php echo $dataKode['jumlah_rusak_cup'];?> Cup</td>
                                  </tr>
                                  </tbody>
                                    <?php  	$no++;	}
									
									
									?>
                                 <tbody>
                                <tr class="beda">
                                   																											
								</tr>
							</tbody>
						</table>
                        </div>
<!------------------------------------------------------------------tabel bawahnya------------------------------------------------------------->
                        <div class="table">		
                        <table>
							<thead>
								<tr>
									<th width="20" class="selected"><input type="checkbox" class="checkall" /></th>
									<th width="20" class="action">No.</th>
                                    <th width="90" class="left">Tanggal Isi</th>
									<th width="82">Jenis Barang</th>	
                                    <th width="82">Cup Ok Dari RTS</th>
                                    <th width="82">Cup Rusak Dari RTS</th>									
								</tr>
						  </thead>
							<tbody>
							<?php			

							$q2=$q;

							if($field=='id_prod'){
								$sqlFormula = mysql_query("SELECT id_produksi FROM tb_produksi WHERE kode_prod LIKE '%$q%'");
								$onoFormula = mysql_num_rows($sqlFormula);
								//echo "SELECT id_formula FROM tb_formula WHERE kode LIKE '%$q%'";
								while ($dataFormula = mysql_fetch_array($sqlFormula)) {
									$qx .= $dataFormula['id_produksi'].',';
								}

								if($onoFormula)
									$q2 = substr($qx, 0, -1);
								else
									$q2 = '999999999999999999999';

							}							
							
							$addQuery='';


							if(!empty($field)){
								if($field=='id_prod')
									$addQuery .= " WHERE $field IN ($q2) ";
								else
									$addQuery .= " WHERE $field LIKE '%$q2%' ";
							}

							$batas=5;
							$pg=(int) $_GET['pg'];
							if(empty($pg))
							{
								$posisi=0;
								$pg=1;
							}
							else
							{
								$posisi = ($pg-1) * $batas;
							}
							$sqlKode1 = mysql_query("SELECT
tb_broken.tgl_isi,
tb_broken.jumlah_cup_ok AS a,
tb_broken.jumlah_rusak_cup,
tb_broken.id_jns_brg
FROM tb_broken  ORDER BY id_broken  DESC limit $posisi, $batas");
							
							$noq=$posisi+1;
							while($dataKode = mysql_fetch_array($sqlKode1)){								

								
							?>
								<tr>
									<td class="selected"><input type="checkbox" /></td>
									<td class="center"><?php echo $noq;?></td>
									
                                    <td style="background-color: #CFC"><?php echo $dataKode['tgl_isi'];?></td>
                                    <?php $nama=mysql_fetch_array(mysql_query("SELECT nama_brg FROM tb_jns_barang WHERE id_jns_brg='$dataKode[id_jns_brg]'"))?>
                                    <td  style="background-color:#CFC"><?php echo $nama['nama_brg'];?></td>
                                    <td style="background-color: #CFC"><?php echo $dataKode['a'];?> Cup</td>			
                                    <td style="background-color: #CFC"><?php echo $dataKode['jumlah_rusak_cup'];?> Cup</td>																															
								</tr>
							<?php
							$noq++;	}

							$sqlSup2 = mysql_query("SELECT tb_broken.jumlah_rusak_cup a, tb_reject.jumlah_rusak_cup b
FROM tb_broken	LEFT JOIN tb_reject ON (tb_broken.id_broken=tb_reject.id_reject) AND tb_broken.status='1' AND tb_reject.status='1'
ORDER BY id_broken DESC");
							$jumlahData = mysql_num_rows($sqlSup2);

							$file="?pages=brg_perbaikan&field=$field&q=$q";
							$jmlhalaman=ceil($jumlahData/$batas);
	
							?>															
							</tbody>
					  </table>
						<!-- pagination -->
						<div class="pagination pagination-left">
							<div class="results">
								<span><?php echo "Showing Results ".($posisi+1)."-".($no-1)." of $jumlahData";?></span>
							</div>
							<ul class="pager">
								<?php
								//link ke halaman sebelumnya (previous)
								if($pg > 1){
									$previous=$pg-1;
									echo "<li><a href=$file&pg=1>&laquo; First</a></li>
									<li><a href=$file&pg=$previous>&laquo; Prev</a></li> ";
								}else{ 
									echo "<li class='disabled'>&laquo; First</li>
									<li class='disabled'>&laquo; Prev</li> ";
								}
								?>

								<?php
								//Link untuk Angka
								$angka=($pg > 3 ? " <li class='separator'>...</li> " : " ");
								for($i=$pg-2;$i<$pg;$i++)
								{
								  if ($i < 1) 
								      continue;
								  $angka .= "<li><a href='$file&pg=$i'>$i</a></li> ";
								}

								$angka .= " <li class='current'>$pg</li>";
								for($i=$pg+1;$i<($pg+3);$i++)
								{
								  if ($i > $jmlhalaman) 
								      break;
								  $angka .= "<li><a href='$file&pg=$i'>$i</a></li> ";
								}

								$angka .= ($pg+2<$jmlhalaman ? " <li class='separator'>...</li> <li><a href='$file&pg=$jmlhalaman'>$jmlhalaman</a></li>" : " ");

								echo "$angka";
								?>
								

								<?php
								//link kehalaman berikutnya (Next)
								if($pg < $jmlhalaman)
								{
									$next=$pg+1;
									echo "<li><a href='$file&pg=$next'>Next &raquo;</a></li>
									<li><a href='$file&pg=$jmlhalaman'>Last &raquo;</a></li>";
								}
								else
								{ 
									echo "<li class='disabled'>Next &raquo;</li>
									<li class='disabled'>Last &raquo;</li> ";
								}		
								?>													
							</ul>
						</div>

						<!-- end pagination -->
						<!-- table action -->
						
						<!-- end table action -->	

						<div style="margin:30px auto">
								<?php
							$sqlTemp = mysql_query("SELECT * FROM tb_temp_rts a, tb_jns_barang b WHERE a.id_jns_brg=b.id_jns_brg");
							while($dataTemp = mysql_fetch_array($sqlTemp)){
							$u++;
							echo '<h5>'.$u.'. '.$dataTemp[nama_brg].'</h5><br>';
							?>
							<form method="POST" action="?pages=brg_perbaikan&act=update">
								<table style="width:auto">
								   	<tbody>
								   		<tr>
											<td><label><span class="left">Total yg di perbaiki</span></label></td>
											<td>:</td>
											<td><input type="text" value="<?php echo $dataTemp['stock_cup_sehat'];?>" size="20" name="total" id="tot_sisa" readonly style="background-color: #ffd76e"><span class="add-on"> Cup</span></td>
                                            <td><label>Tgl Perbaikan</label></td>
                                            <td>:</td>
                                            <td><input type="text"  value="<?php echo $dataSup['tgl_perbaikan'];?>" size="30" name="tgl_perbaikan" id="tgl_perbaikan" placeholder="Tgl. Perbaikan" required="required"/></td>
									   	</tr>	
                                        
									   	<tr>
											<td><label><span class="left">Packing ke stock perbaikan </span></label></td>
											<td>:</td>
											<td><input type="text"  size="20" name="to_stok_perbaikan" id="karton" required>
									    <span class="add-on"> Karton</span></td>											
											<td>Isi Per Karton</td>
											<td>:</td>
											<td><input type="text"  size="20" name="isi" id="cup" required><span class="add-on"> Cup</span></td>
											<td><input type="submit" value="Simpan" name="submit" class="btn btn-primary"></td>											
									   	</tr>																   
									</tbody>
								</table>								
							</form>
                            <?php
							}
							?>
						</div>					
					</div>
                    
                    

<?php
}elseif (isset($_POST['submit'])) {
	# code...
	$now 					=$_POST['tgl_perbaikan'];
	$total 					= $_POST['total'];
	$to_stok_perbaikan 		= $_POST['to_stok_perbaikan'];
	$isi 					= $_POST['isi'];
	
	$nilai= $to_stok_perbaikan * $isi;
	$nilai2=$total - $nilai;
	
	if($nilai > $total){
				sukses('Data Perbaikan Anda Melebihi Jumlah Total yang diperbaiki', '?pages=brg_perbaikan&act=edit');
			}else{

		$update_temp_rts=mysql_query("SELECT * from tb_temp_rts");
		$update_temp_rts2=mysql_fetch_array($update_temp_rts);
		$temp=$update_temp_rts2["stock_perbaikan"];
		$temp2=$update_temp_rts2["stock_cup_sehat"];

		$total_temp=$temp + $to_stok_perbaikan;
		//$total_temp2=$temp2 - $nilai2;

		$karton="Karton";
		$cup="Cup";

		$sqlInsert = mysql_query("INSERT INTO tb_stock_reject values (NULL,'$now','$to_stok_perbaikan','$karton','$isi','$cup')");
		$sqlUpdate1= mysql_query("UPDATE tb_temp_rts SET stock_perbaikan='$total_temp',stock_cup_sehat='$nilai2' WHERE id_temp='1'");
		$sqlUpdate2= mysql_query("UPDATE tb_broken SET status='0' WHERE status='1'");
		$sqlUpdate3= mysql_query("UPDATE tb_reject SET status='0' WHERE status='1'");
		
		if($sqlInsert && $sqlUpdate1 && $sqlUpdate2 && $sqlUpdate3)
			sukses('Data berhasil disimpan..', '?pages=brg_perbaikan');
		
	}
}
?>					